//
//  RegionHospitalDeskViewController.m
//  安好
//
//  Created by lianchuang on 14-12-8.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "RegionHospitalDeskViewController.h"

//筛选的右边的下拉列表--选等级
#import "RightTableCell.h"

#import "RegDocDeskHospitalCell.h"

#define LeftWeight ([UIScreen mainScreen].bounds.size.width/2.0)
#define RGB(r, g, b)    [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:1.f]
#define iPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)

@interface RegionHospitalDeskViewController ()<UITableViewDelegate,UITableViewDataSource,FKSearchBarDelegate>{

    FKSearchBar * illSeachBar;
}

/**
 *  左边的table
 */
@property(nonatomic,strong)UITableView * LeftTableView;
/**
 *  右边的table
 */
@property(nonatomic,strong)UITableView * RightTableView;

/**
 *  右边的数据
 */
@property(nonatomic,strong)NSMutableArray * rightDataArray;
/**
 * 左边的数据
 */
@property(nonatomic,strong)NSMutableArray * leftDataArray;
/**
 *  左table页面选中的省modal
 */
@property(nonatomic,strong)AreaModal * provinceModal;
/**
 * 左table页面选中的 父科室modal
 */
@property(nonatomic,strong)RegistrationDepartmentModal * departmentModal;

/**
 *  下拉加载页数
 */
@property (assign, nonatomic) NSInteger loadPage;

/**
 *  下拉右面的加载页数
 */
@property (assign, nonatomic) NSInteger loadRightPage;

#pragma mark ------- 只有过来选医院才会用到下面的属性
/**
 * 点击筛选后下拉下的view，把筛选列表添加到这个view上
 */
@property(nonatomic,strong)UIView * filtrateTopView;
/**
 *  筛选等级下拉列表
 */
@property(nonatomic,strong)UITableView * filtrateTableView;
/**
 *  筛选等级下拉数组
 */
@property(nonatomic,strong)NSArray * levelDataArray;
/**
 * 等级字典 {@“三级甲等”:@"2"}...
 */
@property(nonatomic,strong)NSMutableDictionary * levelDic;
/**
 * 添加等级名字的lable
 */
@property(nonatomic,strong)UILabel * levelNameLable;
/**
 *  是否在搜索
 */
@property (assign, nonatomic) BOOL isSearch;
/**
 * 搜索框输入的关键字
 */
@property(nonatomic,copy)NSString *searchhospital;
/**
 * 请求医院的时候--把参数数添加到这里
 */
@property(nonatomic,strong)NSDictionary * afnetParDic;
/**
 *  筛选下拉选中cell
 */
@property(nonatomic,strong)RightTableCell * selectCell;
/**
 *  半透明的图片 -- 当点击升起pickerView的时候 这个也sheng起来
 *  筛选按钮触发后下拉视图下拉，这个半透明的背景视图也下来
 */
@property(nonatomic,strong)UIView * alphaView;

@property(nonatomic,assign)CGFloat filtHeight;






@end

@implementation RegionHospitalDeskViewController
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // 取消输入框
    [illSeachBar.searchTextField resignFirstResponder];
    [self setEditing:NO animated:YES];
    
    [illSeachBar.searchTextField removeTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    [[AFHTTPRequestOperationManager manager].operationQueue cancelAllOperations];
    [self hiddenActionView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.searchArray = [[NSMutableArray alloc]init];
   
    if (iPhone6Plus) {
        self.filtHeight = 240;
    }else{
        self.filtHeight = 200;
    }

    [self makeNavBar];
    
    [self makeTableView];
    [self creatSearchTableView];

    [self loadRequest];
    
    
    

}
#pragma mark - 导航条设置
-(void)makeNavBar
{
    self.navigationItem.title = self.navBarTitle;
    //左按钮--返回
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    [backBtnImg setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = backBtnImg;
    
}

#pragma mark - 创建筛选按钮--只有在选医院的时候才回创建筛选
/**
 *  创建-筛选-按钮
 */
-(void)makeRightFilterItem
{
    UIBarButtonItem *filterBtnItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_筛选.png"] style:UIBarButtonItemStylePlain target:self action:@selector(filterBtnDown)];
    filterBtnItem.tag = 1000;
    filterBtnItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = filterBtnItem;
}
/**
 *  点击筛选按钮--触发下拉左右table 同时 调用创建“完成”按钮
 */
-(void)filterBtnDown{
    [illSeachBar.searchTextField resignFirstResponder];
    [self makeRightFilterFinishItem];
    [self makeAlphaView];
    [self.view bringSubviewToFront:self.filtrateTopView];
    [UIView animateWithDuration:0.25 animations:^{
        self.filtrateTopView.frame = CGRectMake(0, 0,kScreenWidth , self.filtHeight);
        self.alphaView.alpha = 0.35;
        
    }];
}

/**
 *  创建“完成”按钮
 */
-(void)makeRightFilterFinishItem
{
    UIBarButtonItem *filterBtnItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_完成.png"] style:UIBarButtonItemStylePlain target:self action:@selector(filterFinishDown)];
    filterBtnItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = filterBtnItem;
    
}
/**
 *  点击完成按钮--收回下拉筛选列表--把半透明view移除 --网络请求
 */
-(void)filterFinishDown{

    [UIView animateWithDuration:0.25 animations:^{
        self.filtrateTopView.frame = CGRectMake(0, -self.filtHeight, kScreenWidth, self.filtHeight);
        self.alphaView.alpha = 0;
    } completion:^(BOOL finished) {
        [self makeRightFilterItem];
        [self.alphaView removeFromSuperview];
    }];

    self.loadPage = 1;
    NSString * level = self.levelDic[self.selectCell.nameLable.text];
    self.afnetParDic = @{@"provinceid":self.getProvinceModal.provinceid,@"cityid":self.getCityModal.cityid,@"level":level};
    [self AFNetRequestGetHospitalWithProvinceModal];
    self.afnetParDic = @{@"provinceid":self.getProvinceModal.provinceid,@"cityid":self.getCityModal.cityid};
}
/**
 * 创建下拉筛选的下拉列表 和 等级的数据
 */
-(void)makeFilterTopView{
    
    self.levelDataArray =  @[@"全部",@"三级特等",@"三级甲等",@"三级乙等",@"三级丙等",@"二级甲等",@"二级乙等",@"二级丙等",@"一级甲等",@"一级乙等",@"一级丙等",@"对外专科",@"其他"];
    NSArray * dengjiId = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];
    self.levelDic = [NSMutableDictionary dictionaryWithCapacity:0];
    for (NSInteger i = 0; i < self.levelDataArray.count; i++) {
        [self.levelDic setValue:dengjiId[i] forKey:self.levelDataArray[i]];
    }
    
    //筛选的底部图片
    self.filtrateTopView = [[UIView alloc] initWithFrame:CGRectMake(0, -self.filtHeight, kScreenWidth,self.filtHeight)];
   
    self.filtrateTopView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.filtrateTopView];
    
    //筛选的右tableView
    self.filtrateTableView = [[UITableView alloc] initWithFrame:CGRectMake(LeftWeight, 0, LeftWeight,self.filtrateTopView.height ) style:UITableViewStylePlain];
    self.filtrateTableView.delegate = self;
    self.filtrateTableView.dataSource = self;
    self.filtrateTableView.showsHorizontalScrollIndicator = NO;
    self.filtrateTableView.showsVerticalScrollIndicator = NO;
    self.filtrateTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.filtrateTableView.backgroundColor = RGB(238, 238, 238);
    [self.filtrateTopView addSubview:self.filtrateTableView];
    //筛选的左边的
    UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, LeftWeight, 35)];
    if (iPhone6Plus) {
         backView.frame = CGRectMake(0, 12, LeftWeight, 42);
    }
    backView.backgroundColor = RGB(238, 238, 238);
    [self.filtrateTopView addSubview:backView];
    UIView * topLine = [[UIView alloc] initWithFrame:CGRectMake(LeftWeight-0.5, 0, 0.5, 10)];
    if (iPhone6Plus) {
         topLine.frame = CGRectMake(LeftWeight-0.5, 0, 0.5, 12);
    }
    topLine.backgroundColor = [ClassMethodsViewController colorWithHexString:@"#cccccc"];;
    [self.filtrateTopView addSubview:topLine];
    UIView * botLine = [[UIView alloc] initWithFrame:CGRectMake(LeftWeight-0.5, 45, 0.5, self.filtrateTopView.height-45)];
    if (iPhone6Plus) {
        botLine.frame = CGRectMake(LeftWeight-0.5, 54, 0.5, self.filtrateTopView.height-54);
    }
    botLine.backgroundColor = [ClassMethodsViewController colorWithHexString:@"#cccccc"];;
    [self.filtrateTopView addSubview:botLine];
    //“等级”
    UILabel * levelLable = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 60, 35)];
    
    levelLable.font = [UIFont systemFontOfSize:13];
    if (iPhone6Plus) {
        levelLable.frame =CGRectMake(20, 0, 60, 42);
        levelLable.font = [UIFont systemFontOfSize:15];
    }
    levelLable.textAlignment = NSTextAlignmentCenter;
    levelLable.text = @"等级";
    [backView addSubview:levelLable];
    //“等级名字”
    self.levelNameLable = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 60, 35)];
    self.levelNameLable.font = [UIFont systemFontOfSize:13];
    if (iPhone6Plus) {
        self.levelNameLable.frame = CGRectMake(80, 0, 60, 42);
        self.levelNameLable.font = [UIFont systemFontOfSize:15];
    }
    self.levelNameLable.textAlignment = NSTextAlignmentCenter;
    self.levelNameLable.text = self.levelDataArray[0];
    [backView addSubview:self.levelNameLable];
    
}

#pragma - mark 搜索--searchBar
- (void)markIllnessSearchBar
{
    illSeachBar = [[FKSearchBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
    illSeachBar.searchTextField.placeholder = @"医院";
    illSeachBar.searchTextField.delegate = self;
    illSeachBar.fkSearchRequestDelegate = self;
    [self.view addSubview:illSeachBar];
    [illSeachBar.searchTextField addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];

}
#pragma - mark FKSearchDelegate
/**
 *  FKSearch的delegate
 */
- (void)VoiceConversion
{
    [illSeachBar.searchTextField resignFirstResponder];
    [self starVoice];
}
- (void)voice:(NSString *)voice
{
    illSeachBar.searchTextField.text = voice;
    self.isSearch = YES;
    self.loadPage = 1;
    self.searchhospital = voice;
    [self AFNetRequestGetHospitalWithProvinceModal];
}
- (void)SearchString
{
    [illSeachBar.searchTextField resignFirstResponder];
    
    self.isSearch = YES;
    self.loadPage = 1;
    self.searchhospital = illSeachBar.searchTextField.text;
    [self AFNetRequestGetHospitalWithProvinceModal];
}



#pragma mark - 返回按钮 -- 以上是--“医院”过来筛选

-(void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}
/**
 * 从上个页面过来分别做请求
 */
-(void)loadRequest
{
    
    self.leftDataArray = [NSMutableArray arrayWithCapacity:0];
    self.rightDataArray = [NSMutableArray arrayWithCapacity:0];
    
    if ([self.loadRequestNum intValue] == 0) {
        
        //加载地区
        [self AFNetRequestForProvinceAndCity:self.provinceid];
    }
    //请求医院
    if([self.loadRequestNum intValue] == 1)
    {
        
        //从医院过来会有搜索--加载搜索框
        [self markIllnessSearchBar];
        //创建筛选按钮
        [self makeRightFilterItem];
        //创建下拉列表和创建数据--这个过来不会显示--坐标都在负值
        [self makeFilterTopView];
        //改变tableView的fram--以和搜索框对应
        self.LeftTableView.frame = CGRectMake(0,44, kScreenWidth, kScreenHeight-iOS7TopMargin-44);
        self.loadPage = 1;
        self.afnetParDic = @{@"provinceid":self.getProvinceModal.provinceid,@"cityid":self.getCityModal.cityid,@"level":@"0"};
        //把搜索框添加上
        [self AFNetRequestGetHospitalWithProvinceModal];
        
    }
    //请求科室
    if ([self.loadRequestNum intValue] == 2) {
        self.loadPage = 1;
        [self AFNetRequestGetDepartment];

    }

}

#pragma mark - 这个请求某一个省下面的所有的市
//传“0”就是获取到所有的省，点击省的时候，把该省的provinceid传过来请求该省下面的所有市
-(void)AFNetRequestForProvinceAndCity:(NSString *)provinceid
{
    [self showActionView];
    
    NSString *postURL = [NSString stringWithFormat:@"%@guahao/area/",kBaseURL];
    //post参数列表
    NSDictionary *parameters = @{@"platform": kPlatform,@"version":kVersion,@"provinceid":provinceid};
    //网络请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //开始请求
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        [self hiddenActionView];
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
             [self shouLabel:@"网络失败"];
        }else
        {
            [self.rightDataArray removeAllObjects];
            for (NSDictionary * dic in responseObject[@"data"]) {
                AreaModal * modal = [[AreaModal alloc] initWithDic:dic];
                if ([provinceid intValue] == 0) {
                    [self.leftDataArray addObject:modal];
                    [self.LeftTableView reloadData];
                    self.LeftTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
                }else
                {
                    [self.rightDataArray addObject:modal];
                    [self.RightTableView reloadData];
                }
                
            }
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        MyNSLog("失败");
        [self hiddenActionView];
    }];


}
#pragma mark - AFNet获取某地区的医院
//获取该市下面的所有的医院，需要传省id，市id。获取到医院
-(void)AFNetRequestGetHospitalWithProvinceModal
{
    [self showActionView];

    NSMutableDictionary * afparDic = [NSMutableDictionary dictionaryWithCapacity:0];
    [afparDic setDictionary:self.afnetParDic];
    [afparDic setValue:kPlatform forKey:@"platform"];
    [afparDic setValue:kVersion forKey:@"version"];
    [afparDic setValue:[NSString stringWithFormat:@"%ld",self.loadPage] forKey:@"page"];
    //如果点击搜索
    if (self.isSearch == YES) {
        //关键字不为空
        if ([illSeachBar.searchTextField.text length] != 0) {
            //把参数中添加 关键字参数
            [afparDic setValue:self.searchhospital forKey:@"keyword"];
        }
    }
    
    NSString *postURL = [NSString stringWithFormat:@"%@guahao/hospital/",kBaseURL];

    //网络请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //开始请求
    [manager POST:postURL parameters:afparDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        [self hiddenActionView];
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
            MyNSLog("%@", responseObject[@"msg"]);
            if (self.loadPage == 1) {
                if (self.isSearch == YES && [responseObject[@"data"] count]==0 ) {
                    [self.leftDataArray removeAllObjects];
                    [self.LeftTableView reloadData];
                }else{
                    if ([responseObject[@"data"] count]==0) {
                        [self.leftDataArray removeAllObjects];
                        [self.LeftTableView reloadData];
                    }
                }
                [self shouLabel:@"暂无内容"];
            }
            //
            [self.LeftTableView footerEndRefreshing];
        }else
        {
            [self hiddenLabel];
            self.LeftTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
            
            NSArray * hospitalArray = responseObject[@"data"];
            MyNSLog("%d", hospitalArray.count);
            //如果这个网络请求是正在搜索过来的
            if (self.isSearch == YES)
            {
                //判断数组是否为空--》需要清空数据--重新获取
                if ([self.leftDataArray count] != 0){
                    [self.leftDataArray removeAllObjects];
                }
            }
            //筛选的时候需要将loadPage==1
            if (self.loadPage == 1) {
                [self.leftDataArray removeAllObjects];
            }

            if (hospitalArray != nil )
            {
                self.loadPage ++;
                for (NSDictionary * dic in hospitalArray) {
                    RegistrationHospitalModal * modal = [[RegistrationHospitalModal alloc] initWithDic:dic];
                    [self.leftDataArray addObject:modal];
                }
                [self.LeftTableView reloadData];
            }
            else if([hospitalArray count]< 20)
            {
            
                [self.LeftTableView footerEndRefreshing];
            }
            else
            {
                if (self.loadPage > 1) {
                    MyNSLog("下拉无数据");
                }
            }
            [self.LeftTableView footerEndRefreshing];
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        MyNSLog("失败");
        [self hiddenActionView];
        [self.LeftTableView footerEndRefreshing];
    }];
    

}
#pragma mark - 获取某医院下面的科室
//在上面的请求中获取到医院，在这个请求中获取到该医院下面的科室，传参数医院id
-(void)AFNetRequestGetDepartment
{
    [self showActionView];
    
//    NSString *postURL = [NSString stringWithFormat:@"%@wiki/hospitalDepartment",kBaseURL];
    
    NSString *postURL = [NSString stringWithFormat:@"%@guahao/HospitalDepartment/",kBaseURL];
    //post参数列表
    NSDictionary *parameters = @{@"platform": kPlatform,@"version":kVersion,@"hospitalid":self.getHospitalModal.hospitalid,@"parenthosdepid":@"0",@"page":[NSString stringWithFormat:@"%ld",self.loadPage]};
    //网络请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //开始请求
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        [self hiddenActionView];
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
            if (self.loadPage == 1 ) {
                [self shouLabel:@"暂无内容"];
            }
            [self.LeftTableView footerEndRefreshing];
        }else
        {
            self.LeftTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
            
            MyNSLog("%@", responseObject[@"data"]);
            NSArray * deskArray = responseObject[@"data"];
            if (self.loadPage == 1) {
                [self.leftDataArray removeAllObjects];
                [self.LeftTableView reloadData];
            }
            if (deskArray != nil) {
                self.loadPage ++;
                for (NSDictionary * dic in deskArray) {
                    RegistrationDepartmentModal * modal = [[RegistrationDepartmentModal alloc] initWithDic:dic];
                    [self.leftDataArray addObject:modal];
                }
                [self.LeftTableView reloadData];
                
            }else if (deskArray.count < 20 ){
                [self.LeftTableView footerEndRefreshing];
            
            }else
            {
                if (self.loadPage>1) {
                    MyNSLog("上拉无数据");
                }
            }
             [self.LeftTableView footerEndRefreshing];

        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        MyNSLog("失败");
        [self hiddenActionView];
         [self.LeftTableView footerEndRefreshing];
    }];

}
#pragma mark - 获取二级科室
-(void)AFNetRequestForChildDepartmend
{
    [self showActionView];
    MyNSLog("loadRightPage %d", self.loadRightPage);
    NSString *postURL = [NSString stringWithFormat:@"%@guahao/HospitalDepartment/",kBaseURL];
    MyNSLog("hospdeptid : %@",self.departmentModal.hospdeptid );
    
    //post参数列表
    NSDictionary *parameters = @{@"platform": kPlatform,@"version":kVersion,@"hospitalid":self.getHospitalModal.hospitalid,@"page":[NSString stringWithFormat:@"%ld",self.loadRightPage],@"parenthosdepid":self.departmentModal.hospdeptid};
    //网络请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        [self hiddenActionView];
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
         MyNSLog("%@", responseObject);
        if ([statusString isEqualToString:@"0"]) {
            if (self.loadRightPage == 1 ) {
                [self shouLabel:@"暂无内容"];
            }

            [self.RightTableView footerEndRefreshing];
        }else
        {
            MyNSLog("%@", responseObject[@"data"]);
            NSArray * childDeskArray = responseObject[@"data"];
            if (self.loadRightPage == 1) {
                [self.rightDataArray removeAllObjects];
                [self.RightTableView reloadData];
            }
            if (childDeskArray != nil) {
                self.loadRightPage ++;
                for (NSDictionary * dic in childDeskArray) {
                    RegistrationDepartmentModal * modal = [[RegistrationDepartmentModal alloc] initWithDic:dic];
                    [self.rightDataArray addObject:modal];
                }
                [self.RightTableView reloadData];
                
            }else if (childDeskArray.count < 20 ){
                [self.RightTableView footerEndRefreshing];
                
            }else
            {
                if (self.loadPage>1) {
                    MyNSLog("上拉无数据");
                }
            }
            [self.RightTableView footerEndRefreshing];
            
        }
            
        
    } failure:
    ^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [self.RightTableView footerEndRefreshing];
        
    }];

}

-(void)makeTableView
{
    //左边的table
    self.LeftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-iOS7TopMargin)style:UITableViewStyleGrouped];
    [self.view addSubview:self.LeftTableView];
    self.LeftTableView.delegate = self;
    self.LeftTableView.dataSource = self;
    self.LeftTableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    //隐藏横向和竖向的滑条
    self.LeftTableView.showsHorizontalScrollIndicator = NO;
    self.LeftTableView.showsVerticalScrollIndicator = NO;
    self.LeftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.LeftTableView setSeparatorColor:RGB(221, 221, 221)];
    if ([self.loadRequestNum intValue] == 1) {
        [self.LeftTableView addFooterWithTarget:self action:@selector(AFNetRequestGetHospitalWithProvinceModal)];
    }
    if ([self.loadRequestNum intValue] == 2) {
        [self.LeftTableView addFooterWithTarget:self action:@selector(AFNetRequestGetDepartment)];
    }

    //右边的table
    self.RightTableView = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight-iOS7TopMargin)];
    [self.view addSubview:self.RightTableView];
    self.RightTableView.delegate = self;
    self.RightTableView.dataSource = self;
    self.RightTableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    //隐藏横向和竖向的滑条
    self.RightTableView.showsHorizontalScrollIndicator = NO;
    self.RightTableView.showsVerticalScrollIndicator = NO;
    self.RightTableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    [self.RightTableView setSeparatorColor:RGB(221, 221, 221)];
    self.RightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.RightTableView.layer.backgroundColor =[ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16].CGColor;
    if ([self.loadRequestNum intValue] == 2) {
        [self.RightTableView addFooterWithTarget:self action:@selector(AFNetRequestForChildDepartmend)];
    }
    
    
}
- (void)creatSearchTableView{
    self.searchTable = [[UITableView alloc]initWithFrame:CGRectMake(0,44, kScreenWidth, kScreenHeight - iOS7TopMargin - 44) style:UITableViewStyleGrouped];
    [self.view addSubview:self.searchTable];
    self.searchTable.delegate = self;
    self.searchTable.dataSource = self;
    self.searchTable.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    self.searchTable.showsHorizontalScrollIndicator = NO;
    self.searchTable.showsVerticalScrollIndicator = NO;
    self.searchTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.searchTable setSeparatorColor:RGB(221, 221, 221)];
    self.searchTable.hidden = YES;


    
}

//段尾的高
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (tableView == self.filtrateTableView) {
        
        if (iPhone6Plus) {
            return 6;
        }else{
            return 5;
        }
    }else{
        return 0.01;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView == self.filtrateTableView) {
        if (iPhone6Plus) {
            return 12;
        }else{
            return 10;
        }
    }else{
        return 0.01;
    }
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (tableView == self.filtrateTableView) {
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LeftWeight, 10)];
        if (iPhone6Plus) {
            view.height = 12;
        }
        view.backgroundColor = RGB(238, 238, 238);
        return view;
    }else
    {
        return nil;
    }

}
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (tableView == self.filtrateTableView) {
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LeftWeight, 5)];
        if (iPhone6Plus) {
            view.height = 6;
        }
        view.backgroundColor = RGB(238, 238, 238);
        return view;
    }
    else{
        return nil;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchTable) {
        return self.searchArray.count;
    }
    else if (tableView == self.LeftTableView) {
        return self.leftDataArray.count;
    }
    else if (tableView == self.RightTableView) {
        return self.rightDataArray.count;
    }
    else if (tableView == self.filtrateTableView){
        return self.levelDataArray.count;
    }
    else{
        return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchTable) {
        return 35;
    }
    else if (tableView == self.RightTableView || tableView == self.LeftTableView) {
        if ([self.loadRequestNum intValue] == 1) {
            if (iPhone6Plus) {
                return 78;
            }else{
                return 65;
            }
        }else{
            if (iPhone6Plus) {
                return 53;
            }else{
                return 44;
            }
        }
    }
    else if (tableView == self.filtrateTableView){
        if (iPhone6Plus) {
            return 42;
        }else{
            return 35;
        }
    }
    else{
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == self.searchTable){
            static NSString *reuseIdetify = @"SvTableViewCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdetify];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdetify];
            }
            cell.textLabel.font = [UIFont systemFontOfSize:14];
            cell.backgroundColor = RGB(238, 238, 238);
            cell.textLabel.text = [self.searchArray objectAtIndex:indexPath.row];
            return cell;
    }
    else if (tableView == self.LeftTableView) {
        //如果不是选中的医院
        if ([self.loadRequestNum intValue] != 1) {
            UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LeftID"];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LeftID"];
            }
            cell.textLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT];
            cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
            cell.selectedBackgroundView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
            
            //请求地区
            if ([self.loadRequestNum intValue] == 0) {
                AreaModal * modal = self.leftDataArray[indexPath.row];
                cell.textLabel.text = modal.name;
                
            }
            
            //请求的是科室
            if ([self.loadRequestNum intValue] == 2) {
                RegistrationDepartmentModal * modal = self.leftDataArray[indexPath.row];
                NSString * subString = modal.departmentname;
                for (NSInteger i = 0; i < subString.length; i++) {
                    char ch = [subString characterAtIndex:i];
                    if (ch != '\n' && ch != '\r') {
                        subString = [subString substringFromIndex:i];
                        break;
                    }
                }
                cell.textLabel.text = subString;
                
            }
            
            return cell;
        }
        else//过来选医院
        {
            RegDocDeskHospitalCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
            if (cell == nil) {
                if (iPhone6Plus) {
                    cell = [[[NSBundle mainBundle] loadNibNamed:@"RegDocDeskHospitalCell" owner:self options:nil] lastObject];
                }else{
                    cell = [[[NSBundle mainBundle] loadNibNamed:@"RegDocDeskHospitalCell" owner:self options:nil] firstObject];
                }
            }
          
            cell.userImageView.clipsToBounds = YES;
            cell.userImageView.layer.cornerRadius = 3;
            cell.leaveLabel.left = cell.userImageView.left;
            RegistrationHospitalModal * modal = self.leftDataArray[indexPath.row];
            cell.userLabel.text = modal.hospitalname;
            cell.userLabel.width = kScreenWidth - 15- cell.userLabel.left;
            cell.goodLabel.text = modal.address;
            cell.goodLabel.width = cell.userLabel.width;
            cell.leaveLabel.text = modal.levelname;
            [cell.userImageView setImageWithURL:[NSURL URLWithString:modal.picurl] placeholderImage:[UIImage imageNamed:@"医院默认"]];
            cell.bottomLineView.hidden = YES;
            
            return cell;
        }
        
    }
    else if(tableView == self.RightTableView){
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RightID"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RightID"];
        }
        cell.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
        cell.textLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        if ([self.loadRequestNum intValue] == 0) {
            if (self.rightDataArray.count != 0) {
                AreaModal * modal = self.rightDataArray[indexPath.row];
                cell.textLabel.text = modal.name;
            }
        }
        
        if ([self.loadRequestNum intValue] == 2) {
            if (self.rightDataArray.count != 0) {
                RegistrationDepartmentModal * modal = self.rightDataArray[indexPath.row];
                NSString * subString = modal.departmentname;
                for (NSInteger i = 0; i < subString.length; i++) {
                    char ch = [subString characterAtIndex:i];
                    if (ch != '\n' && ch != '\r') {
                        subString = [subString substringFromIndex:i];
                        break;
                    }
                }
                cell.textLabel.text = subString;
            }
        }
        
        return cell;
        
    }
    else if (tableView == self.filtrateTableView){
        RightTableCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Filtrate"];
        if (cell == nil) {
            if (iPhone6Plus) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"RightTableCell" owner:self options:nil] lastObject];
            }else{
                cell = [[[NSBundle mainBundle] loadNibNamed:@"RightTableCell" owner:self options:nil] firstObject];}
        }
        if (indexPath.row == 0) {
            self.selectCell = cell;
        }
        cell.contentView.backgroundColor = RGB(238, 238, 238);
        cell.nameLable.text = self.levelDataArray[indexPath.row];
        
        return cell;
    }
    else{
        return nil;
    }
        
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==self.searchTable) {
        
        illSeachBar.searchTextField.text = [self.searchArray objectAtIndex:indexPath.row];
        [self.view endEditing:YES];
        [self SearchString];
        
        return;
    }
    //点击的时左边的tableView
    else if (tableView == self.LeftTableView) {
       
        //点击的是地区
        if ([self.loadRequestNum intValue] == 0) {
            [UIView animateWithDuration:0.35 animations:^{
                self.LeftTableView.frame = CGRectMake(0, 0, kScreenWidth/2.0-30, kScreenHeight-iOS7TopMargin);
                self.RightTableView.frame = CGRectMake( self.LeftTableView.width,0, kScreenWidth-self.LeftTableView.width, kScreenHeight-iOS7TopMargin);
            }];
            //获取到该行的省id
            AreaModal * modal = self.leftDataArray[indexPath.row];
            self.provinceModal = modal;
            //网络请求把该省的id传过去，请求该省下面的所有的市
            [self AFNetRequestForProvinceAndCity:modal.provinceid];
        }
        
        
        //点击的是医院--这里医院没有科室选择，只有一个tableView
        if ([self.loadRequestNum intValue] == 1) {
            
                //获取到该医院的modal
                RegistrationHospitalModal * modal = self.leftDataArray[indexPath.row];
                [self.delegate getAreaProvinceModal:self.getProvinceModal andCityModal:self.getCityModal andHospitalModal:modal andParentDepartmentModal:nil andChildDepartment:nil];
                [self.navigationController popViewControllerAnimated:YES];
        
        }
        //点击的是父科室-
        if ([self.loadRequestNum intValue] == 2) {
            MyNSLog("%d", indexPath.row);
            //把父科室传上去
            self.departmentModal =self.leftDataArray[indexPath.row];
            [UIView animateWithDuration:0.35 animations:^{
                self.LeftTableView.frame = CGRectMake(0, 0, kScreenWidth/2.0-30, kScreenHeight-iOS7TopMargin);
                self.RightTableView.frame = CGRectMake( self.LeftTableView.width,0, kScreenWidth-self.LeftTableView.width, kScreenHeight-iOS7TopMargin);
            }];


            self.loadRightPage = 1;
            [self.rightDataArray removeAllObjects];
            [self.RightTableView reloadData];
            [self AFNetRequestForChildDepartmend];
        }
        
    }
    //点击的时右tableView，这个tableView只有在选择医院的时候不出像
    else if(tableView == self.RightTableView)
    {
        //点击的是地区
        if ([self.loadRequestNum intValue] == 0) {
            //这是cityModal
            AreaModal * modal = self.rightDataArray[indexPath.row];
            //把这人地区modal反向传值到上一个页面
            [self.delegate getAreaProvinceModal:self.provinceModal andCityModal:modal andHospitalModal:nil andParentDepartmentModal:nil andChildDepartment:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }
        //点击获取子科室
        if ([self.loadRequestNum intValue] == 2) {
            MyNSLog("子科室  %d",indexPath.row );
            RegistrationDepartmentModal * modal = self.rightDataArray[indexPath.row];
            [self.delegate getAreaProvinceModal:self.getProvinceModal andCityModal:self.getCityModal andHospitalModal:self.getHospitalModal andParentDepartmentModal:self.departmentModal andChildDepartment:modal];
            [self.navigationController popViewControllerAnimated:YES];
            
        }
    }
    else{
        MyNSLog("%d", indexPath.row);
        self.levelNameLable.text = self.levelDataArray[indexPath.row];
        RightTableCell * cell = (RightTableCell*)[tableView cellForRowAtIndexPath:indexPath];
        self.selectCell = cell;
        
    }

}
#pragma mark - 创建半透明的view
-(void)makeAlphaView
{
    self.alphaView = [[UIView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth, kScreenHeight-iOS7TopMargin)];
    self.alphaView.backgroundColor = [UIColor blackColor];
    self.alphaView.alpha = 0;
    [self.view addSubview:self.alphaView];
    //给这个半透明的View添加一个手势
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alphaViewTapClicked:)];
    [self.alphaView addGestureRecognizer:tap];
}
-(void)alphaViewTapClicked:(UITapGestureRecognizer*)tap{

    [UIView animateWithDuration:0.25 animations:^{
        self.filtrateTopView.frame = CGRectMake(0,-self.filtHeight, kScreenWidth, self.filtHeight);
        self.alphaView.alpha = 0;
    } completion:^(BOOL finished) {
        [self makeRightFilterItem];
        [self.alphaView removeFromSuperview];
    }];
    self.afnetParDic = @{@"provinceid":self.getProvinceModal.provinceid,@"cityid":self.getCityModal.cityid,@"level":@"0"};
    
    
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (!self.LeftTableView.hidden) {
        self.LeftTableView.hidden = YES;
        self.searchTable.hidden = NO;
        
    }
    
    return YES;
    
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (!self.searchTable.hidden) {
        self.LeftTableView.hidden = NO;
        self.searchTable.hidden = YES;
        
        
    }
}
- (void)textFieldEditChanged:(UITextField *)textField
{
    if (!self.searchTable.hidden) {
        
        [self loadsearchData:textField.text];
    }
    MyNSLog("textField text : %@", [textField text]);
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (!self.searchTable.hidden) {
        
        [self SearchString];
        
    }
    return YES;
}
-(void)loadsearchData:(NSString*)text{
    
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
    [dict setObject:kPlatform forKey:@"platform"];
    [dict setObject:kVersion forKey:@"version"];
    [dict setValue:text forKey:@"q"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [[manager operationQueue] cancelAllOperations];
    [manager POST:[NSString stringWithFormat:@"%@%@",kBaseURL,AUTO_HOSPITAL] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        [self hiddenActionView];
        [self.searchArray removeAllObjects];
        
        if ([statusString isEqualToString:@"0"]) {
        }else{
            
            NSArray *jsonDictArr = [[responseObject objectForKey:@"data"] isEqual:[NSNull null]]?nil:[responseObject objectForKey:@"data"];
            MyNSLog("%@", jsonDictArr);
            
            for (NSDictionary *objc in jsonDictArr) {
                [self.searchArray addObject:objc];
            }
        }
        [self.searchTable reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
        [self.searchTable reloadData];
        
    }];
    
}

@end
